mozillafandomcom-20200213-history
Stats/ua script
< Stats Tracking Proposed PHP scripts Kincera's Script This is the code donated by the wonderful Kincera. Any alterations, please leave the original code intact and format your proposed alterations so as to keep them obvious. Any discussion, please use the discussion page. Thanks must go out again to Kincera. A-dead-trousers' Script This script has been submitted by A-dead-trousers. A working version of it can bee seen in action at http://universe.dotgeek.org/counter/index.php The source code can also been downloaded from there as well. This script includes images which may be used by webmasters. Feel free to comment. --awrowe 09:39, 2 Apr 2005 (EST) hi! Just forgot to mention that I used the ua_script by Kincera. I'm going to insert a notice in the readme file! --A-dead-trousers 16:47, 3 Apr 2005 (CEST) Proposed Additions "Transparent" script behind image The following would be added to the top of any script that doesn't return information to the browser. This would prevent the user from having to waste precious miliseconds while the rest of the script executes. Submitted by Adam A. (Ghedipunk) This addition would allow the script to be processed every time that the selected image is loaded. An example of how a webmaster would include this into a page is as follows: Sorry, but absolutely useless, cause php won't send anything before it is done! The stream output of php isn't recommended yet because of side effects and is deactivated by default! (Don't know any Webhoster where it is activated) --A-dead-trousers 13:57, 4 Apr 2005 (CEST) Proposed Changes Change $database variable for compatibility I propose to change "$database" to something that wouldn't be confused as a different global variable found in different server configurations... for example, "$sfx_statcount_db" or more simply, "$db", since people have had problems with the original variable name. (Submitted by Ghedipunk before he learned how to timestamp) :Agreed. Perhaps we should go through to find any other variables which CMS' might find confusing also.--awrowe 10:47, 1 Apr 2005 (EST) Use switch() instead of if() where possible To make the code cleaner, perhaps faster (I don't know the specific speeds of the code), a main "switch" statement should be used instead of bunches of "if" statements. switch($agent){ case "opera": $val = stristr($agent, "opera"); if (eregi("/", $val)){ $val = explode("/",$val); $bd'browser' = $val0; $val = explode(" ",$val1); $bd'version' = $val0; }else{ $val = explode(" ",stristr($val,"opera")); $bd'browser' = $val0; $bd'version' = $val1; } break; case "webtv": $val = explode("/",stristr($agent,"webtv")); $bd'browser' = $val0; $bd'version' = $val1; break; /* and so on */ } :I'm not sure if this code is any faster, although I have known it to be more manageable rather than using tons of if statements.--codyrockx 16:07, 1 Apr 2005 (PST) :This looks fantastic. If you wouldn't mind, would it be possible for you to complete it, or if not, would you mind if anyone else did? Thanks very much for putting it in! --awrowe 07:46, 2 Apr 2005 (EST) :I don't think this will work, cause $agent will be set during the if-statments and can't be checked by a switch structure --A-dead-trousers 13:30, 4 Apr 2005 (CEST) Proposed Alternatives File storage instead of database This is a possible alternative that could be presented to webmasters who do not have MySQL available, or who do not want one of their databases used up. This script is meant to replace the database access method in Kincera's script, and all of the changes that I made fall right under the AOL detection block of code. Please note that I'm assuming the script will be placed in the document root of the web server, and referencing a file that is one directory above the document root... this way, people can't just type out "www.yourserver.com/stats.txt", and see your stats... they'd already need administrative privileges to your site before viewing this file. // get date and time $d = getdate(); $secs = time(); $t = seconds_to_time($secs); // finally assign our properties $Name = $bd'browser'; $Version = $bd'version'; $Platform = $bd'platform'; $AOL = $bd'aol'; $thisdate = $d'month' . "-" . $d'mday' . "-" . $d'year'; $thistime = $t'hours' . ":" . $t'minutes' . ":" . $t'seconds'; $ip_addr=$HTTP_SERVER_VARS"REMOTE_ADDR"; //******************** // // Write to file // //******************** if (file_exists('../stats.txt') ) { $statfile = fopen('../stats.txt', r); $filecontents = fread($statfile, filesize('../stats.txt')); fclose($statfile); $filecontents = $filecontents . " " . $thisdate . " " . $thistime . " " . $Name . " " . $Version . " " . $Platform . " " . $ip_addr; } else { $filecontents = "Unconfigured UA Stats datafile -- For a Spread Firefox project " . $thisdate . " " . $thistime . " " . $Name . " " . $Version . " " . $Platform . " " . $ip_addr; } $statfile = fopen('../stats.txt', w); fwrite($statfile, $filecontents); fclose($statfile); //******************** // // Function to make time more human readable // //******************** function seconds_to_time ($seconds) { $retArr'years' = floor ($seconds / 31536000); if ($retArr'years' > 1) $seconds -= $retArr'years' * 31536000; $retArr'days' = floor ($seconds / 86400); if ($retArr'days' > 1) $seconds -= $retArr'days' * 86400; $retArr'hours' = floor ($seconds / 3600); if ($retArr'hours' > 1) $seconds -= $retArr'hours' * 3600; $retArr'minutes' = floor ($seconds / 60); if ($retArr'minutes' > 1) $seconds -= $retArr'minutes' * 60; $retArr'seconds' = $seconds; return $retArr; } ?> Here is the output from my tests so far: (I'd include AOL, but the AOL client ignores the hosts file, and I haven't uploaded this file to a live server for testing yet) Unconfigured UA Stats datafile -- For a Spread Firefox project April-2-2005 16:12:29 Firefox 1.0.2 Windows 127.0.0.1 April-2-2005 16:12:33 Firefox 1.0.2 Windows 127.0.0.1 April-2-2005 16:12:35 Firefox 1.0.2 Windows 127.0.0.1 April-2-2005 16:12:52 MSIE 6.0 Windows 127.0.0.1 April-2-2005 16:12:57 MSIE 6.0 Windows 127.0.0.1 If anybody is wondering why I placed "Unconfigured UA stats..." on the top line, it is because I eventually want to make an installation script that allows a webmaster to choose their type of storage method (file, MySQL database, some other database, etc.), as well as the method that will be used to push these stats up to the main server (SMTP, RSS, etc.). All times are GMT because that's why my server runs on. Ghedipunk 10:32, 2 Apr 2005 (EST) :Will be included in my next version! Will only work at low traffic (not recommended for central servers), cause of concurrent access to the file and filelocks! --A-dead-trousers 13:44, 4 Apr 2005 (CEST) :Possible workaround for filelocks: Using BerkleyDB which runs on a filebase and just post the DB-File --A-dead-trousers 12:25, 7 Apr 2005 (CEST) UA Script